<template>
  <div class='container'>
  <el-form  ref="form" :model="sim"  label-width="80px" :rules="rules">
<el-form-item v-if="!query.name" label="所属学科" prop="subjectID">
  <el-select v-model="sim.subjectID" placeholder="请选择" style="width:456px">
      <el-option v-for="item in simples" :key="item.value" :label="item.label" :value="item.value"></el-option>
    </el-select>
    </el-form-item>
<el-form-item label="目录名称" prop="directoryName">
<el-input v-model="sim.directoryName" placeholder="请输入目录名称"></el-input>
</el-form-item>

<el-form-item class="but">
    <el-button @click="$emit('dialog')">取 消</el-button>
    <el-button type="primary" @click="submit">确 定</el-button>
</el-form-item>
  </el-form>
  </div>
</template>

<script>
import { add, detail, update } from '../../api/hmmm/directorys'
import { simple } from '../../api/hmmm/subjects'

export default {
  props: {
    query: {
      type: Object,
      required: true
    },
    isEdit: {
      type: Boolean,
      required: true
    },
    id: {
      type: Number,
      require: false
    }
  },
  data () {
    return {
      sim: {
        subjectID: this.query.id || '', // 新增目录学科
        directoryName: '', // 新增目录名称
        state: ''
      },
      simples: [],
      rules: {
        directoryName: [{ required: true, message: '请输入目录名称', trigger: 'blur' }],
        subjectID: [{ required: true, message: '请选择活动区域', trigger: 'change' }]
      }
    }
  },
  created () {
    if (this.sgg) {
      this.sim = this.sgg
    }
  },
  methods: {
    // 确认提交
    submit () {
      this.$refs.form.validate(async val => {
        // console.log(this.sim)
        if (!val) return
        try {
          if (this.isEdit) {
            await update(this.sim)
            this.$emit('dialog')
            // this.dialogVisible = falses
            this.$emit('dir')
            // this.directorys()
            return this.$message.success('修改成功')
          }
          if (this.query.name) {
            await add({ directoryName: this.sim.directoryName, subjectID: +this.sim.subjectID })
          } else {
            await add(this.sim)
          }
          this.$emit('dir')
          // this.directorys()
          this.$message.success('增加成功')
          this.$emit('dialog')
          this.dialogVisible = false
        } catch (e) {
          console.log(e)
        }
      })
    },
    // 关闭弹窗，清空表单
    hclose () {
      this.$refs.form.clearValidate()
      this.sim = {
        subjectID: '', // 新增目录学科
        directoryName: '', // 新增目录名称
        state: ''
      }
    },
    // 修改
    async  amendd () {
      const res = await simple()
      const res1 = await detail(this.id)
      this.simples = res.data
      this.sim = res1.data
    },
    // 增加
    async  haddedit () {
      const res = await simple()
      this.simples = res.data
    }
  }
}
</script>

<style scoped lang='less'></style>
